package com.xxx.mch.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.xxx.mch.annotation.WithRole;
import com.xxx.mch.domain.dto.DepartmentScoreSummary;
import com.xxx.mch.domain.dto.KaopinDto;
import com.xxx.mch.domain.po.Department;
import com.xxx.mch.domain.po.DepartmentReward;
import com.xxx.mch.domain.request.KaopinRequest;
import com.xxx.mch.exception.BusinessException;
import com.xxx.mch.service.impl.DepartmentServiceImpl;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 部门管理
 *
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020-02-25
 */
@RestController
@RequestMapping ("department")
@Api (tags = "部门信息")
public class DepartmentController
{

    @Autowired
    private DepartmentServiceImpl departmentService;


    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DepartmentReward>> getList(@RequestParam ("month") String month)
    {
        return WrapMapper.ok(departmentService.getDepartmentReward(month));
    }


    @PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> update(@RequestBody List<DepartmentReward> departmentRewardList)
    {
        departmentService.insertOrUpdateReward(departmentRewardList);
        return WrapMapper.ok();
    }


    /**
     * 读取配置文件中指定机构代码的下属机构列表
     *
     * @return
     */
    @GetMapping (value = "subsidiarie", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<Department>> getOrganization()
    {
        List<Department> list = departmentService.insertOrUpdate();
        return WrapMapper.ok(list);
    }


    /**
     * 获取部门考评
     */
    @WithRole (needRoles = {"KPI_DEFAULT_kh_pd_deptscore"})
    @ApiOperation (value = "获取部门考评")
    @GetMapping (value = "kaoPin", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<KaopinDto>> kaoPin(@RequestParam ("month") String month)
    {
        List<KaopinDto> kaoPinDtoList = departmentService.getKaopin(month);
        return WrapMapper.ok(kaoPinDtoList);
    }

    @ApiOperation (value = "修改部门锁")
    @PutMapping (value = "lock/{deptId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper updateDeptLock(@ApiParam(name = "month", value = "月份：2021-01", required = true) @RequestParam ("month") String month,
                                  @PathVariable Integer deptId,
                                  @ApiParam(name = "status", value = "状态：1锁定", required = true) @RequestParam Integer status)
    {
        return WrapMapper.ok(departmentService.updateDeptLock(deptId, month, status));
    }


    /**
     * 修改部门考评
     */
    @WithRole (needRoles = {"KPI_DEFAULT_kh_pd_deptscore"})
    @ApiOperation (value = "修改部门考评")
    @PostMapping (value = "kaoPin", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<KaopinDto>> kaoPin(@RequestParam ("month") String month, @RequestBody List<KaopinRequest> kaoPinRequestList)
    {
        departmentService.updateKaoPin(month, kaoPinRequestList);
        return WrapMapper.ok(departmentService.getKaopin(month));
    }


    /**
     * 下载部门考评结果
     */
    @WithRole (needRoles = {"KPI_DEFAULT_kh_pd_deptscore"})
    @ApiOperation (value = "下载部门考评结果")
    @GetMapping (value = "kaoPin/download", produces = MediaType.APPLICATION_JSON_VALUE)
    public void kaoPinResultDownload(@RequestParam ("month") String month, HttpServletResponse response)
    {
        try
        {
            List<KaopinDto> kaoPinDtoList = departmentService.getKaopin(month);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition",
                               "attachment;filename=" + URLEncoder.encode("部门考评结果" + month, "UTF-8") + ExcelTypeEnum.XLSX.getValue());
            EasyExcel.write(response.getOutputStream(), KaopinDto.class).sheet(month + "部门考评结果").doWrite(kaoPinDtoList);
        }
        catch (IOException e)
        {
            throw new BusinessException("生成excel失败");
        }
    }


    /**
     * 读取配置文件中指定机构代码的下属机构列表
     *
     * @return
     */
    @GetMapping (value = "list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<Department>> getDeptList()
    {
        List<Department> list = departmentService.getDeptList();
        return WrapMapper.ok(list);
    }


    @ApiOperation (value = "查询得分汇总信息，注意：排序前端框架排序，后端不处理")
    @GetMapping (value = "scoreSummary", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DepartmentScoreSummary>> scoreSummary(@RequestParam ("month") String month)
    {
        List<DepartmentScoreSummary> list = departmentService.scoreSummary(month);
        return WrapMapper.ok(list);
    }


    @ApiOperation (value = "查询得分汇总信息,传部门id表示查指定部门，不传查询时不限定部门")
    @GetMapping (value = "user/scoreSummary", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<LinkedHashMap<String, String>>> usersScoreSummary(@RequestParam ("month") String month,
                                                                          @RequestParam (value = "deptId", required = false) Integer deptId)
    {
        return WrapMapper.ok(departmentService.usersScoreSummary(month, deptId));
    }

    @ApiOperation (value = "获取部门锁状态")
    @GetMapping (value = "lock/{deptId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper getDeptLock(@ApiParam(name = "month", value = "月份：2021-01", required = true) @RequestParam ("month") String month,
                                  @PathVariable Integer deptId)
    {
        return WrapMapper.ok(departmentService.getDeptLock(deptId, month));
    }
}
